/**************************************************************************
 *
 *   Copyright 2010 American Public Media Group
 *
 *   This file is part of AIR2.
 *
 *   AIR2 is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   AIR2 is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with AIR2.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************/

Ext.ns('AIR2.Import.Resolve');
/***************
 * AIR2 Import Conflict Resolution Row component
 *
 * A container with 3 columns... the current value, resolution buttons, and
 * the tank value.
 *
 * @class AIR2.Import.Resolve.ConflictRow
 * @extends Ext.Container
 * @xtype air2conflict
 * @cfg {String} model
 * @cfg {Ext.XTemplate} tpl
 * @cfg {String} currTitle (default: {model})
 * @cfg {String} currUrl
 * @cfg {String} currEmptyText
 * @cfg {String} newTitle (default: 'Conflicting Value')
 * @cfg {Object} newData
 * @cfg {String} validOps -- 'REPLACE|IGNORE|ADD'
 *
 */
AIR2.Import.Resolve.ConflictRow = function(cfg) {
    if (!cfg.currTitle) cfg.currTitle = cfg.model;
    if (!cfg.newTitle) cfg.newTitle = 'Conflicting Value';

    cfg.items = [{
        columnWidth: .42,
        cls: 'column-left',
        xtype: 'fieldset',
        title: cfg.currTitle,
        autoLoad: cfg.currUrl,
        html: cfg.currEmptyText,
        renderer: {
            render: function(el, resp) {
                var data = Ext.decode(resp.responseText);
                if (data && data.radix) {
                    cfg.tpl.overwrite(el, data.radix);
                }
                else {
                    el.update("A remote error occured!!");
                }
            }
        }
    },{
        xtype: 'air2resolvegroup',
        columnWidth: .16,
        model: cfg.model
    },{
        xtype: 'fieldset',
        cls: 'column-right',
        columnWidth: .42,
        title: cfg.newTitle,
        data: cfg.newData,
        tpl: cfg.tpl
    }];

    AIR2.Import.Resolve.ConflictRow.superclass.constructor.call(this, cfg);
}
Ext.extend(AIR2.Import.Resolve.ConflictRow, Ext.Container, {
    cls: 'resolve-row',
    layout: 'column',
    setOperations: function() {
        this.get(1).setReplace(this.validOps.indexOf('REPLACE') > -1);
        this.get(1).setIgnore(this.validOps.indexOf('IGNORE') > -1);
        this.get(1).setNew(this.validOps.indexOf('ADD') > -1);
    },
    // track/add conflicts
    conflicts: [],
    addConflict: function(column, msg) {
        this.setOperations();
        if (!this.get(2).rendered) {
            this.get(2).on('afterrender', function() {
                this.addConflict(column, msg);
            }, this, {single:true});
        }
        else {
            var el = this.get(2).el.child('li.'+column);
            if (el) el.addClass('conflict');
        }
    }
});
Ext.reg('air2conflict', AIR2.Import.Resolve.ConflictRow);
